/*
 * @Author: 恒恒 1067531094@qq.com
 * @Date: 2022-10-10 16:50:06
 * @LastEditors: 恒恒 1067531094@qq.com
 * @LastEditTime: 2022-10-10 19:46:50
 * @FilePath: \作业\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//导入 http 模块
const http = require('http')
const persons = require('./persons.json')
const user = require('./user.json')
//创建 http 服务
let server = http.createServer((req, res) => {
    let requrl = req.url
    persons.forEach((item) => {
        if (requrl == '/login') {
            res.writeHead(200, { "content-type": "text/html;charset=utf-8" });
            if (item == user) {
                res.end('登录成功')
            } else {
                res.end('用户名和密码错误')
            }
        } else if (requrl == '/reg') {
            res.writeHead(200, { "content-type": "text/html;charset=utf-8" });
            if (item.username == user.username) {
                res.end('用户名已被占用')
            } else if (item != user) {
                res.end('可以注册')
            }
        }
    })
})
//创建监听窗口
server.listen(9996, () => {
    console.log('服务器启动成功')
})